/**
 * 辅助方法
 */
define(function (require, exports, module) {
    module.exports={
        //验证手机号码
        validatePhone:function (textval) {
            const regx=/^1[3|4|5|7|8][0-9]{9}$/;
            return regx.test(textval);
        },
        //合法url
        validateURL:function(textval) {
            const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
            return urlregex.test(textval);
        },
        param:function(json) {
            if (!json) return '';
            return cleanArray(Object.keys(json).map(function(key){
                if (json[key] === undefined) return '';
                return encodeURIComponent(key) + '=' +
                    encodeURIComponent(json[key]);
            })).join('&');
        },
        param2Obj:function(url) {
            const search = url.split('?')[1];
            if(!search)return {};
            return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
        },
        html2Text:function(val) {
            const div = document.createElement('div');
            div.innerHTML = val;
            return div.textContent || div.innerText;
        },
        scrollTo:function(element, to, duration) {
            if (duration <= 0) return;
            const difference = to - element.scrollTop;
            const perTick = difference / duration * 10;
            setTimeout(function(){
                console.log(new Date());
                element.scrollTop = element.scrollTop + perTick;
                if (element.scrollTop === to) return;
                scrollTo(element, to, duration - 10);
            }, 10);
        },
        oauthUrl:function () {
            var return_url=encodeURIComponent('https://www.us-ebike.com/mobile/');
            var state=new Date().getTime();
            var url='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx43ed8f9d033c629c&redirect_uri='+return_url+'&response_type=code&scope=snsapi_userinfo&state='+state+'#wechat_redirect';
            return url;
        },
        getQrcode:function (str) {
            //str type1 "CODE_128,010000112"
            //str type2 "http://www.us-ebike.com/download/app.html?id=010000113"
            if(!str)return "";
            if(/^http/i.test(str)){
                const search = str.split('?')[1];
                const params=JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
                return params.id;
            }else if(/^code/i.test(str)){
                return str.split(',')[1];
            }
            else {
                return str;
            }
        },
        isWeixn:function() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                return true;
            } else {
                return false;
            }
        }
}
});